package com.xxc.hs.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SourceDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "smart_eel_source.db";
    private static final int DATABASE_VERSION = 2; // 版本升级为2

    // 表结构定义
    public static final String TABLE_NAME = "source_records";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCT_NAME = "product_name";
    public static final String COLUMN_PRODUCT_SPEC = "product_spec";
    public static final String COLUMN_PRODUCT_BATCH = "product_batch";
    public static final String COLUMN_FARMING_DATE = "farming_date";
    public static final String COLUMN_FISHING_DATE = "fishing_date";
    public static final String COLUMN_PROCESSING_DATE = "processing_date";
    public static final String COLUMN_TRANSPORTATION_DATE = "transportation_date";
    public static final String COLUMN_SOURCE_INFO = "source_info";
    public static final String COLUMN_INPUT_TIME = "input_time";

    // 创建表的SQL语句
    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_PRODUCT_NAME + " TEXT NOT NULL, " +
                    COLUMN_PRODUCT_SPEC + " TEXT NOT NULL, " +
                    COLUMN_PRODUCT_BATCH + " TEXT NOT NULL, " +
                    COLUMN_FARMING_DATE + " TEXT, " +
                    COLUMN_FISHING_DATE + " TEXT, " +
                    COLUMN_PROCESSING_DATE + " TEXT, " +
                    COLUMN_TRANSPORTATION_DATE + " TEXT, " +
                    COLUMN_SOURCE_INFO + " TEXT NOT NULL, " +
                    COLUMN_INPUT_TIME + " TEXT NOT NULL);";

    public SourceDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            // 升级表结构
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_FARMING_DATE + " TEXT;");
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_FISHING_DATE + " TEXT;");
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_PROCESSING_DATE + " TEXT;");
            db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + COLUMN_TRANSPORTATION_DATE + " TEXT;");
        }
    }
}